/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.admin.client;

import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.AxisFault;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import lk.mrt.cse.wihidum.util.WihidumConstants;

/**
 * Client invoking admin service
 */
public class Client
{
    private UserInterface userInterface;

    public Client(UserInterface userInterface)
    {
        this.userInterface = userInterface;
    }

    public void insertBroker (String adminAddress, String brokerAddress) {
        try {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(adminAddress));
            options.setAction(WihidumConstants.Operations.INSERT_BROKER);
            serviceClient.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMElement messageElement = factory.createOMElement("BrokerDetails", null);
            OMElement addressElement = factory.createOMElement("Address", null);
            addressElement.setText(brokerAddress);
            messageElement.addChild(addressElement);

            OMElement responseElement = serviceClient.sendReceive(messageElement);
            userInterface.showInfo(responseElement.getText());
        }
        catch (NullPointerException ex)
        {
            userInterface.showError("Error in processing Insert Broker response");
        }
        catch (AxisFault axisFault)
        {
            userInterface.showError("Error in sending Insert Broker request");
        }
    }

    public void removeBroker (String adminAddress, String brokerAddress) {
        try {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(adminAddress));
            options.setAction(WihidumConstants.Operations.REMOVE_BROKER);
            serviceClient.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMElement messageElement = factory.createOMElement("BrokerDetails", null);
            OMElement addressElement = factory.createOMElement("Address", null);
            addressElement.setText(brokerAddress);
            messageElement.addChild(addressElement);

            OMElement responseElement = serviceClient.sendReceive(messageElement);
            userInterface.showInfo(responseElement.getText());
        }
        catch (NullPointerException ex)
        {
            userInterface.showError("Error in processing Remove Broker response");
        }
        catch (AxisFault axisFault)
        {
            userInterface.showError("Error in sending Remove Broker request");
        }
    }

    public void changeTopology (String adminAddress, String topology) {
        try {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(adminAddress));
            options.setAction(WihidumConstants.Operations.CHANGE_TOPOLOGY);
            serviceClient.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMElement messageElement = factory.createOMElement("TopologyDetails", null);
            OMElement addressElement = factory.createOMElement("Topology", null);
            addressElement.setText(topology);
            messageElement.addChild(addressElement);

            OMElement responseElement = serviceClient.sendReceive(messageElement);
            userInterface.showInfo(responseElement.getText());
        }
        catch (NullPointerException ex)
        {
            userInterface.showError("Error in processing Change Topology response");
        }
        catch (AxisFault axisFault)
        {
            userInterface.showError("Error in sending Change Topology request");
        }
    }

    public void getMessageTrace (String adminAddress) {
        try {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(adminAddress));
            options.setAction(WihidumConstants.Operations.GET_MESSAGE_TRACE);
            serviceClient.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMElement messageElement = factory.createOMElement("Details", null);
            messageElement.setText("Get Messages");

            OMElement responseElement = serviceClient.sendReceive(messageElement);
            userInterface.showInfo(responseElement.getText());
        }
        catch (NullPointerException ex)
        {
            userInterface.showError("Error in processing Get Message Trace response");
        }
        catch (AxisFault axisFault)
        {
            userInterface.showError("Error in sending Get Message Trace request");
        }
    }

    public void getNetworkMap (String adminAddress) {
        try {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(adminAddress));
            options.setAction(WihidumConstants.Operations.GET_NETWORK_MAP);
            serviceClient.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMElement messageElement = factory.createOMElement("Details", null);
            messageElement.setText("Get Network Map");

            OMElement responseElement = serviceClient.sendReceive(messageElement);
            userInterface.showInfo(responseElement.getText());
        }
        catch (NullPointerException ex)
        {
            userInterface.showError("Error in processing Get Network Map response");
        }
        catch (AxisFault axisFault)
        {
            userInterface.showError("Error in sending Get Network Map request");
        }
    }

}
